Resuming the finite-difference Hessian ====================================== ``hessian(mdlest)`` computes the Hessian of minus the log posterior (minus the log likelihood under ``estim_mle``) at the mode by bounded central finite differences (``rise.engine.numerics.hessian.bounded_central``). It takes of the order of :math:`d^2` evaluations of the posterior kernel for :math:`d` estimated parameters, and on a large model that can outlast the time limit of a job. With ``estim_hessian_checkpoint`` naming a MAT-file, ``hessian`` in RISE (:cite:t:`Maih2015`) keeps its evaluations in that file. The same call in a later job resumes from the file, evaluates the kernel only at the points that remain, and returns the Hessian of an uninterrupted call, bit for bit. The option is empty by default: nothing is then read or written, and the calculation is unchanged. .. contents:: :local: :depth: 1 The call -------- A checkpoint belongs to ``hessian``, not to ``estimate``. Estimate the mode without a Hessian, keep the model, and compute the Hessian in as many jobs as it needs:: % first job: the mode mdlest = estimate(m, 'estim_hessian_type', 'none'); save('mode.mat', 'mdlest'); % every later job: the same two lines load('mode.mat', 'mdlest'); [mdlest, H, issue] = hessian(mdlest, 'estim_hessian_checkpoint', 'curvature.mat'); Resume from the saved model, not from a new mode search: a new search need not end at the same point bit for bit, and the file is then refused. The job in which the call returns has the Hessian. At the mode, ``hessian`` stores it in ``posterior_maximization.hessian``, with ``hessian_source`` ``'finite differences (hessian)'``, and recomputes ``vcov``, ``mode_stdev`` and ``log_marginal_data_density_laplace`` from it, exactly as without a checkpoint. At another point, ``hessian(mdlest, x, ...)`` returns the Hessian and stores nothing. A stopped job returns nothing: there is no partial Hessian. * ``H`` is in nats (natural log units of the posterior) per product of the units of two parameters; its inverse, ``vcov``, is in the products of the parameters' units, and ``mode_stdev`` in the parameters' units. Under linear restrictions ``H`` is in the free coordinates of the objective. * ``issue`` and ``hessian_issue`` report curvature that could not be resolved, as without a checkpoint. * The option holds for one call. The model that ``hessian`` returns has ``estim_hessian_checkpoint`` empty, so it can be estimated again. * An array of models writes one file per model: the name followed by ``_1``, ``_2``, ... * A mode with a parameter on a bound has inward one-sided stencils (``rise.engine.numerics.hessian.bounded_mode``). They are recorded in the same way. What the file holds ------------------- The file keeps the points at which the kernel was evaluated, in the order of the calculation, and the values found there. It holds no partial Hessian and no loop counter. .. list-table:: :header-rows: 1 :widths: 22 78 * - Field - Content * - ``format``, ``version``, ``algorithm`` - ``'RISE bounded_central checkpoint'`` (``'RISE bounded_mode checkpoint'`` at a mode on a bound), the version of the layout, and the stencil * - ``x``, ``lb``, ``ub`` - the point and the bounds of the estimated parameters * - ``options`` - the step settings of ``bounded_central`` (``target``, ``tol``, ``floor``, ``box_share``, ``max_step_iter``, ``penalty``); empty at a mode on a bound * - ``context`` - a 40-character digest of the inputs of the objective (below) * - ``points``, ``values`` - :math:`d \times n` and :math:`1 \times n`: the :math:`n` points evaluated so far and minus the log posterior kernel there, in nats; a failed evaluation is NaN * - ``complete`` - true once the calculation has ended The first point is the point of the Hessian (the mode by default), and its value is minus the log posterior there. Writing and resuming -------------------- * **Writes.** The file is written after the first evaluation that ends at least 60 seconds after the previous write, and at the end of the calculation. Each write goes to a temporary file in the same folder, which then replaces the file whole: a job stopped during a write keeps the previous file. A job stopped between writes loses the evaluations made since the last one; a job stopped before its first write leaves no file. A file that cannot be written gives one warning (``RISE:hessian:checkpointNotWritten``), and the calculation goes on. * **Resume.** The unchanged algorithm runs from the start. Each evaluation it asks for that is in the record is read from the record; the others are computed and added. The steps, the order of the evaluations, the Hessian and ``hessian_info`` are therefore those of an uninterrupted call. * **Cost.** A resumed call evaluates the kernel at the remaining points, and at most twice more, at the point of the Hessian and at the last recorded point, to check the record (below). A complete file costs these two evaluations. * **One file, one calculation.** Two calculations must not write the same file at the same time. What a resume checks -------------------- A file is never ignored. Before it uses a record, ``hessian`` checks it, and any difference is an error that leaves the file as it was. Keep such a file for diagnosis and start the changed calculation under a new name. 1. **The calculation.** The point, the bounds and the step settings must be those of the file. 2. **The objective.** The digest ``context`` is taken over the inputs of the objective: the layout and bounds of the estimated parameters, the values of every parameter, the restrictions, the data, the priors and their hyperparameters, the model's equations and regimes, the Markov chains, every option of the solution, the steady state, the filter and the data (also those that change only the cost of the calculation), and the estimation options that change the objective (``estim_eval_lik``, ``estim_mle``, ``estim_penalty``, ``estim_endogenous_priors``, the restrictions, ...). Display and search settings, ``estim_parallel`` and the model's location on disk are left out: a model saved and loaded again, or moved to another folder, resumes. 3. **The values.** The kernel is evaluated at the point of the Hessian (the mode by default) and at the last recorded point. Both values must equal those of the record within :math:`64\,\epsilon(\max(|\text{live}|,|\text{recorded}|))`, the allowance for the rounding of function values that ``rise.engine.numerics.hessian.curvature_stable`` uses; two failed evaluations (NaN) also agree. The recorded values are the ones used. 4. **The points.** Every evaluation replayed from the record must be asked at the recorded point. The digest names a function given as an option (a steady-state file, for example), not its code. After a change to such code, or to the state it reads, start a new file: the value checks see such a change only where it moves the kernel at their two points. .. list-table:: :header-rows: 1 :widths: 40 60 * - Error - When * - ``RISE:hessian:checkpointMismatch`` - another point, bounds, step settings or objective (the message names the field, ``"context"`` for the objective); a live value that differs from the record; a replayed evaluation at another point * - ``RISE:hessian:checkpointUnreadable`` - the file is not a Hessian checkpoint * - ``RISE:hessian:checkpointUnidentified`` - an input of the objective that the digest cannot read, such as a nested function that captures variables, or a digest that cannot be formed * - ``RISE:hessian:checkpointNeedsFiniteDifferences`` - ``estim_hessian_type = 'opg'``: the outer product of gradients has no checkpoint * - ``estimate:hessianCheckpoint`` - ``estimate`` with the option and an ``estim_hessian_type`` other than ``'none'`` and ``'optimizer'`` ``estimate`` refuses the option when it computes the Hessian itself: a stop during ``estimate`` loses the mode. With ``estim_hessian_type`` ``'none'`` or ``'optimizer'`` it accepts the option and leaves it on the model, for the next ``hessian``. The routines ------------ The model's ``hessian`` passes the checkpoint to the finite-difference routines, which take it for any scalar function:: [H, info] = rise.engine.numerics.hessian.bounded_central(fh, x, lb, ub, options); [H, issue, info] = rise.engine.numerics.hessian.numerical(fh, x, 'fd', lb, ub, options); with ``options.checkpoint``, the file, and ``options.checkpoint_interval``, the seconds between two writes (60 by default; 0 writes after every evaluation). Outside a model there is no digest: a function is identified by the point, the bounds, the step settings and the two value checks only. Example ------- The tutorial ``Estimation/fs2000_pipeline/howto_resumable_hessian.m`` runs the call on the fs2000 model of the estimation tutorial, with its 9 estimated parameters and 192 quarters of data. In the run behind this page: * the Hessian takes 127 evaluations of the kernel, with or without a checkpoint, and the two Hessians are equal bit for bit; * the file holds ``points`` of 9 x 127 and ``values`` of 1 x 127. The first value, -1229.102792, is minus the log posterior at the mode; * the file a job stopped after 42 of the 127 evaluations would leave is resumed from the saved model, as the next job would. The Hessian, ``hessian_info`` and ``mode_stdev`` are those of the uninterrupted call, bit for bit, the Laplace log marginal data density is 1186.716163 in both, and the file ends equal to the record of the uninterrupted call; * the same model on a sample one year shorter is refused with ``"context"``, and the mode with ``alp`` moved by one millionth of its value with ``"x"``. The file is left as it was. See :doc:`Posterior maximization` for the mode search and :doc:`Main Estimation` for the stored posterior outputs.